package com.boot.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;

@Component
public class AdminShutdownHandler {

    private static final Logger log = LoggerFactory.getLogger(AdminShutdownHandler.class);

    @Resource
    private TokenStore tokenStore;

    @PreDestroy
    public void onShutdown() {
        log.info("🎯 应用正在关闭，开始清理管理员 Token...");
        cleanupAdminTokens();
    }

    private void cleanupAdminTokens() {
        try {
            long count = tokenStore.getTokenCountForUser(-1L);
            tokenStore.revokeAllTokensForUser(-1L);
            log.info("✅ 成功清除管理员（userId=-1）的 {} 个 active tokens", count);
        } catch (Exception e) {
            log.error("❌ 清理管理员 tokens 失败", e);
        }
    }
}
